﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DigibizTree.Picker;
using DigibizTree;
using umbraco.cms.businesslogic.media;
using System.Configuration;
using System.Text;
using System.Security.Cryptography;
using System.Xml.XPath;
using umbraco;
using System.Xml.Linq;

namespace DigibizAdvancedMediaPicker
{
    public partial class Pixlr : umbraco.BasePages.BasePage
    {
        protected string IframeUrl { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // media id
                int mediaId;
                if (!int.TryParse(HttpContext.Current.Request.QueryString["id"], out mediaId)) return;

                //Get the media item from the umbraco cache.
                XPathNodeIterator mediaIterator = library.GetMedia(mediaId, false);

                //Convert the XPathNodeIterator to an XDocument.
                XDocument xmlDocument = XDocument.Parse(mediaIterator.Current.OuterXml);

                //Get the root element.
                XElement mediaElement = xmlDocument.Root;

                string umbracoFile = DigibizMediaHelper.GetFilePathElement(mediaElement).Value;
                string text = DigibizMediaHelper.GetMediaName(mediaElement);

                string rootUrl = string.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme,
                                                   HttpContext.Current.Request.Url.Authority,
                                                   HttpContext.Current.Request.ApplicationPath);

                if (rootUrl.EndsWith("/"))
                {
                    rootUrl = rootUrl.Substring(0, rootUrl.Length - 1);
                }


                // pixlr settings
                string umbracoPath = ConfigurationManager.AppSettings["umbracoPath"];
                if (umbracoPath.StartsWith("~"))
                {
                    umbracoPath = umbracoPath.Substring(1);
                }
                string umbracoUrl = rootUrl + umbracoPath;
                string imageUrl = rootUrl + umbracoFile + "?r" + DateTime.Now.ToString("yyyyMMddHHmmss"); // add unique data to avoid pixlr having cached an old image
                string imageTitle = text;
                string exitUrl = umbracoUrl + "/plugins/DigibizAdvancedMediaPicker/SavePixlr.aspx?close=1";
                string mediaKey = DigibizMediaHelper.GetMediaMd5Sum(mediaElement); // add hashed key for security

                string saveUrl = umbracoUrl + "/plugins/DigibizAdvancedMediaPicker/SavePixlr.aspx?id=" + mediaId + "|" + mediaKey;

                IframeUrl = string.Concat("http://pixlr.com/editor/",
                "?image=", HttpContext.Current.Server.UrlEncode(imageUrl),
                "&title=", HttpContext.Current.Server.UrlEncode(imageTitle),
                "&method=GET",
                "&referer=Editor",
                "&exit=", HttpContext.Current.Server.UrlEncode(exitUrl),
                "&target=", HttpContext.Current.Server.UrlEncode(saveUrl),
                "&credentials=True",
                "&locktarget=True",
                "&lockquality=100");
            }
        }
    }
}